package com.cmy.servlet;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

public class FileServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //判断上传的表单是普通表单还是带文件的表单
        if (!ServletFileUpload.isMultipartContent(req)){
            return;//终止方法运行,//说明这是一个普通的表单,直接返回
        }
        //创建上传文件的保存路径,建议在WEB-INF路径下,安全,游客无法直接访问上传的文件
        String uploadPath = this.getServletContext().getRealPath("/WEB-INF/upload");
        File upfile = new File(uploadPath);
        if (!upfile.exists()){//如果不存在创建该目录
            upfile.mkdir();//创建该目录
        }

        //缓存,临时文件(如果文件超过了预期的大小,放到临时文件,过几天自动删除,或者提醒用户转存为永久)
        String tempPath = this.getServletContext().getRealPath("/WEB-INF/temp");
        System.out.println(tempPath);
        File tempFile = new File(tempPath);
        if (!tempFile.exists()){//如果不存在创建该目录
            tempFile.mkdir();//创建该目录
        }

        //处理上传的文件,一般通过流来获取,可以使用req.getInputStream(),比较复杂
        //建议使用apach的文件上传组件,common-fileupload,同时依赖commons-io

        /*
        ServletFIleUpload负责处理上传的文件数据,并将表单中的每个输入项封装成一个FileItem对象
        在使用ServletFileUpload对象解析请求时需要DiskFileItemFactory对象
        所以需要先构造创建DiskFileItemFactory对象
        通过ServletFileUpload对象的构造方法setFileItemFactory()方法设置ServletFileUpload对象的fileItemFactory属性
        */
        //1.创建DiskFIleItemFactory对象,处理文件上传路径和大小限制
        DiskFileItemFactory factory = getDiskFileItemFactory(tempFile);
        //2.获取ServletfileUpload
        ServletFileUpload upload = getServletFileUplad(factory);
        //3.处理上传的文件
        try {
            String msg = uploadParseRequest(upload,req,uploadPath);
            req.setAttribute("msg",msg );
            req.getRequestDispatcher("info.jsp").forward(req,resp);
        } catch (FileUploadException e) {
            e.printStackTrace();
        }

    }
    public static DiskFileItemFactory getDiskFileItemFactory(File tempFile) {
        //1.(必要)创建DiskFileItemFactory对象,处理文件上传路径或者大小限制
        DiskFileItemFactory factory = new DiskFileItemFactory();
        //(有默认值)通过该工厂设置一个缓冲区,当上传的文件大于这个缓冲区时,将其放入临时文件
        factory.setSizeThreshold(1024*1024);//缓存区大小为1M
        factory.setRepository(tempFile);//临时目录的保存目录,需要一个 File类
        return factory;
    }
    public static ServletFileUpload getServletFileUplad(DiskFileItemFactory factory) {
        //2.(必要)获取ServletFileUpload
        ServletFileUpload upload = new ServletFileUpload(factory);

        //(非必须)监听文件上传进度
        upload.setProgressListener(new ProgressListener() {
            @Override
            //pBytesRead:已经读到的文件大小
            //pContentLength:文件大小
            public void update(long pBytesRead, long pContentLength, int pItems) {
                System.out.println("总大小:"+pContentLength+"已上传:"+pBytesRead);
            }
        });

        //(非必须)处理乱码问题
        upload.setHeaderEncoding("UTF-8");
        //设置单个文件的最大值(10M)
        upload.setFileSizeMax(1024*1024*10);
        //设置总共能够上传文件的大小(10M)
        upload.setSizeMax(1024*1024*10);

        return upload;
    }
    public String uploadParseRequest(ServletFileUpload upload, HttpServletRequest req, String uploadPath) throws IOException, FileUploadException {
        String msg = "";
        //3.处理上传文件
        //把前端请求解析,封装成一个FileItem对象,需要从ServletFileUpload对象中获取
        List<FileItem> fileItems = upload.parseRequest(req);
        // fileItem:每一个表单对象
        for (FileItem fileItem : fileItems) {
            //判断上传的文件是普通的表单还是带文件的表单
            if (fileItem.isFormField()){//普通
                //getFileName:前端控件的name;
                String name = fileItem.getFieldName();
                String value = fileItem.getString("UTF-8");//处理乱码
                System.out.println(name+":"+value);
            }else {//文件
                //==================处理文件================//
                String uploadFileName = fileItem.getName();
                //文件名不合法的话:
                if (uploadFileName.trim().equals("")||uploadFileName==null){
                    continue;
                }
                //获取上传的文件名
                String fileName = uploadFileName.substring(uploadFileName.lastIndexOf("/")+1);
                //获取文件的后缀名
                String fileExName = uploadFileName.substring(uploadFileName.lastIndexOf(".")+1);
                //保证文件名唯一:使用UUID类(唯一识别的通用码)
                //UUID.randomUUID(),随机生成一个唯一识别的通用码
                //网络传输的东西 ,都需要序列化
                //JNI (JAVA NATIVE INTERFACE)允许Java代码和其他语言写的代码进行交互,确保代码在不同的平台上方便移植
                String uuidPath = UUID.randomUUID().toString();
                //==================存放地址================//
                //uploadPath 为存储路径
                //文件真实存在的路径realPath
                String realPath = uploadPath+"/"+uuidPath;
                //给每个文件创建一个对应的文件夹
                File realPathFile = new File(realPath);
                if (!realPathFile.exists()){
                    realPathFile.mkdir();
                }
                //==================文件传输================//
                //获取文件上传的流
                InputStream is = fileItem.getInputStream();
                //创建一个文件输出流
                //realPath = 真实的文件夹
                //
                FileOutputStream fos = new FileOutputStream(realPath + "/" + fileName);
                //创建缓冲区
                byte[] buffer = new byte[1024*1024];
                //判断是否读取完毕
                int len = 0;
                while ((len=is.read(buffer))!=-1){
                    fos.write(buffer,0,len);
                }

                //关闭流
                fos.close();
                is.close();

                msg = "文件上传成功";
                fileItem.delete();//上传成功,清除临时文件
            }
        }
        return msg;
    }
}

    